<template>
  <n-card class="creator-stats-card" :bordered="true" size="small">
    <template #header>
      <div class="section-header">
        <n-space align="center" :size="8">
          <n-icon :size="20" color="#e6a23c">
            <BarChartOutline />
          </n-icon>
          <n-text strong>创作者统计</n-text>
        </n-space>
        <n-tag size="small" type="warning">实时</n-tag>
      </div>
    </template>

    <!-- 加载状态 -->
    <div v-if="loading" class="stats-loading">
      <loading-skeleton type="card" :count="4" />
    </div>

    <!-- 统计内容 -->
    <div v-else class="stats-content">
      <!-- 总体统计 -->
      <div class="stats-overview">
        <div class="stats-row">
          <!-- 创作者总数 -->
          <div class="stat-item">
            <div class="stat-icon">
              <n-icon :size="16" color="#409eff">
                <PeopleOutline />
              </n-icon>
            </div>
            <div class="stat-info">
              <div class="stat-value">{{ totalCreators }}</div>
              <div class="stat-label">创作者总数</div>
            </div>
          </div>

          <!-- 推荐创作者 -->
          <div class="stat-item">
            <div class="stat-icon">
              <n-icon :size="16" color="#f56c6c">
                <StarOutline />
              </n-icon>
            </div>
            <div class="stat-info">
              <div class="stat-value">{{ recommendedCreators }}</div>
              <div class="stat-label">推荐创作者</div>
            </div>
          </div>
        </div>

        <!-- 类型分布 -->
        <div class="stats-row">
          <!-- 直播主播 -->
          <div class="stat-item">
            <div class="stat-icon">
              <n-icon :size="16" color="#f56c6c">
                <RadioOutline />
              </n-icon>
            </div>
            <div class="stat-info">
              <div class="stat-value">{{ liveStreamers }}</div>
              <div class="stat-label">直播主播</div>
            </div>
          </div>

          <!-- 视频博主 -->
          <div class="stat-item">
            <div class="stat-icon">
              <n-icon :size="16" color="#409eff">
                <VideocamOutline />
              </n-icon>
            </div>
            <div class="stat-info">
              <div class="stat-value">{{ videoCreators }}</div>
              <div class="stat-label">视频博主</div>
            </div>
          </div>
        </div>

        <!-- 全能创作者 -->
        <div class="stats-row">
          <div class="stat-item full-width">
            <div class="stat-icon">
              <n-icon :size="16" color="#67c23a">
                <TrophyOutline />
              </n-icon>
            </div>
            <div class="stat-info">
              <div class="stat-value">{{ bothTypeCreators }}</div>
              <div class="stat-label">全能创作者</div>
            </div>
          </div>
        </div>
      </div>

      <!-- 分布图表 -->
      <div class="stats-chart">
        <div class="chart-title">类型分布</div>
        <div class="chart-bars">
          <!-- 直播主播比例 -->
          <div class="chart-bar">
            <div class="bar-label">直播</div>
            <div class="bar-container">
              <div 
                class="bar-fill live-bar" 
                :style="{ width: livePercentage + '%' }"
              ></div>
            </div>
            <div class="bar-value">{{ livePercentage }}%</div>
          </div>

          <!-- 视频博主比例 -->
          <div class="chart-bar">
            <div class="bar-label">视频</div>
            <div class="bar-container">
              <div 
                class="bar-fill video-bar" 
                :style="{ width: videoPercentage + '%' }"
              ></div>
            </div>
            <div class="bar-value">{{ videoPercentage }}%</div>
          </div>

          <!-- 全能创作者比例 -->
          <div class="chart-bar">
            <div class="bar-label">全能</div>
            <div class="bar-container">
              <div 
                class="bar-fill both-bar" 
                :style="{ width: bothPercentage + '%' }"
              ></div>
            </div>
            <div class="bar-value">{{ bothPercentage }}%</div>
          </div>
        </div>
      </div>
    </div>
  </n-card>
</template>

<script setup lang="ts">
import { ref, computed, onMounted } from 'vue'
import { 
  BarChartOutline,
  PeopleOutline, 
  StarOutline,
  RadioOutline,
  VideocamOutline,
  TrophyOutline
} from '@vicons/ionicons5'
import { getCreatorsList } from '@/api/creators'
import type { CreatorDetail, CreatorsResponse } from '@/types/creators'
import LoadingSkeleton from '@/components/common/LoadingSkeleton.vue'

// 状态
const loading = ref(false)
const stats = ref({
  total: 0,
  recommended: 0,
  liveStreamers: 0,
  videoCreators: 0,
  bothTypes: 0
})

// 计算属性
const totalCreators = computed(() => stats.value.total)
const recommendedCreators = computed(() => stats.value.recommended)
const liveStreamers = computed(() => stats.value.liveStreamers)
const videoCreators = computed(() => stats.value.videoCreators)
const bothTypeCreators = computed(() => stats.value.bothTypes)

// 百分比计算
const livePercentage = computed(() => {
  if (totalCreators.value === 0) return 0
  return Math.round((liveStreamers.value / totalCreators.value) * 100)
})

const videoPercentage = computed(() => {
  if (totalCreators.value === 0) return 0
  return Math.round((videoCreators.value / totalCreators.value) * 100)
})

const bothPercentage = computed(() => {
  if (totalCreators.value === 0) return 0
  return Math.round((bothTypeCreators.value / totalCreators.value) * 100)
})

// 获取创作者统计数据
const fetchCreatorStats = async () => {
  try {
    loading.value = true
    
    // 获取所有创作者数据进行统计
    const response = await getCreatorsList({ limit: 1000 }) // 获取大量数据用于统计
    
    if (response.code === 1) {
      const creators = (response.data as unknown as CreatorsResponse).list || []
      
      // 统计各种类型
      const statsData = {
        total: creators.length,
        recommended: creators.filter((c: CreatorDetail) => c.is_recommend === 1).length,
        liveStreamers: creators.filter((c: CreatorDetail) => c.creator_type === 1).length,
        videoCreators: creators.filter((c: CreatorDetail) => c.creator_type === 2).length,
        bothTypes: creators.filter((c: CreatorDetail) => c.creator_type === 3).length
      }
      
      stats.value = statsData
    } else {
      console.error('获取创作者统计失败:', response.msg)
    }
  } catch (error: any) {
    console.error('获取创作者统计失败:', error)
  } finally {
    loading.value = false
  }
}

// 组件挂载时获取数据
onMounted(() => {
  fetchCreatorStats()
})
</script>

<style scoped>
.creator-stats-card {
  height: 100%;
}

.section-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  width: 100%;
}

.stats-loading {
  min-height: 200px;
}

.stats-content {
  display: flex;
  flex-direction: column;
  gap: 16px;
}

.stats-overview {
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.stats-row {
  display: flex;
  gap: 8px;
}

.stat-item {
  flex: 1;
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 8px;
  background: var(--n-color-hover);
  border-radius: 6px;
  min-height: 48px;
}

.stat-item.full-width {
  flex: none;
  width: 100%;
}

.stat-icon {
  flex-shrink: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  width: 32px;
  height: 32px;
  background: rgba(255, 255, 255, 0.1);
  border-radius: 50%;
}

.stat-info {
  flex: 1;
  min-width: 0;
}

.stat-value {
  font-size: 16px;
  font-weight: 600;
  color: var(--n-text-color);
  line-height: 1.2;
}

.stat-label {
  font-size: 12px;
  color: var(--n-text-color-2);
  line-height: 1.2;
}

.stats-chart {
  padding-top: 8px;
  border-top: 1px solid var(--n-border-color);
}

.chart-title {
  font-size: 13px;
  font-weight: 500;
  color: var(--n-text-color);
  margin-bottom: 8px;
}

.chart-bars {
  display: flex;
  flex-direction: column;
  gap: 6px;
}

.chart-bar {
  display: flex;
  align-items: center;
  gap: 8px;
}

.bar-label {
  width: 32px;
  font-size: 12px;
  color: var(--n-text-color-2);
  text-align: right;
}

.bar-container {
  flex: 1;
  height: 12px;
  background: var(--n-color-hover);
  border-radius: 6px;
  overflow: hidden;
}

.bar-fill {
  height: 100%;
  border-radius: 6px;
  transition: width 0.3s ease;
}

.live-bar {
  background: #f56c6c;
}

.video-bar {
  background: #409eff;
}

.both-bar {
  background: #67c23a;
}

.bar-value {
  width: 32px;
  font-size: 12px;
  color: var(--n-text-color-2);
  text-align: left;
}

/* 移动端适配 */
@media (max-width: 768px) {
  .stats-row {
    flex-direction: column;
    gap: 6px;
  }

  .stat-item {
    min-height: 40px;
    padding: 6px;
  }

  .stat-value {
    font-size: 14px;
  }

  .stat-label {
    font-size: 11px;
  }
}
</style>
